// SnapIn.cpp : Implementation of CSnapIn
#pragma once
#include "stdafx.h"
#include "MMC_2_0_i.h"
#include "SnapIn.h"
#include "atlbase.h"



/////////////////////////////////////////////////////////////////////////////
// CSimpleComponentData
//DEFINE_GUID  
// {3BDBF00F-0466-4957-9AD8-11161434BAF1}
static const GUID CSnapInGUID_NODETYPE = 
{ 0x3bdbf00f, 0x466, 0x4957, { 0x9a, 0xd8, 0x11, 0x16, 0x14, 0x34, 0xba, 0xf1 } };

const GUID*  CData::m_NODETYPE = &CSnapInGUID_NODETYPE;
const OLECHAR* CData::m_SZNODETYPE = OLESTR("3BDBF00F-0466-4957-9AD8-11161434BAF1");
const OLECHAR* CData::m_SZDISPLAY_NAME = OLESTR("SnapIn");
const CLSID* CData::m_SNAPIN_CLASSID = &CLSID_SnapIn;

HRESULT CData::OnExpand(BOOL bExpand, IConsole *pConsole, HSCOPEITEM parent)
{
	if (!bExpand)
	   return S_OK;

	m_scopeDataItem.ID = parent;
	HSCOPEITEM hChild = NULL;
	long cookie = 0;
	CComQIPtr<IConsoleNameSpace, &IID_IConsoleNameSpace> spConsoleNameSpace(pConsole);
	spConsoleNameSpace->GetChildItem(m_scopeDataItem.ID, &hChild, &cookie);
	if (hChild != NULL)
	  spConsoleNameSpace->DeleteItem(m_scopeDataItem.ID, FALSE);

	m_pServer->m_scopeDataItem.relativeID = m_scopeDataItem.ID;
	HRESULT hr = spConsoleNameSpace->InsertItem(&m_pServer->m_scopeDataItem);
	_ASSERT( SUCCEEDED(hr) );
	
	//m_pMsgNode->m_scopeDataItem.relativeID = m_scopeDataItem.ID;
	//hr = spConsoleNameSpace->InsertItem(&m_pMsgNode->m_scopeDataItem);
	//_ASSERT( SUCCEEDED(hr) );

	m_pConAxNode->m_scopeDataItem.relativeID = m_scopeDataItem.ID;
	hr = spConsoleNameSpace->InsertItem(&m_pConAxNode->m_scopeDataItem);
	_ASSERT( SUCCEEDED(hr) );
	return S_OK;
//after this MMC calls GetDisplayInfo() which 
//in ATL translates to GetScopePaneInfo()
}

//HRESULT STDMETHODCALLTYPE CSnapIn::GetHelpTopic(/* [out] */ LPOLESTR __RPC_FAR *lpCompiledHelpFile)
//{
//	USES_CONVERSION;	
//	if ( !lpCompiledHelpFile)
//		return E_POINTER; // invalid argument 
//	*lpCompiledHelpFile = (LPOLESTR) CoTaskMemAlloc( MAX_PATH * sizeof(WCHAR) ); 
//	if ( !lpCompiledHelpFile )
//		return E_OUTOFMEMORY;
//	
//	#pragma chMSG(Change the help path to valid path)
//	ocscpy(*lpCompiledHelpFile, T2OLE("Help\\SimpleSnapIn.chm"));
//	return S_OK;
//}

HRESULT CSnapIn::Initialize(LPUNKNOWN pUnknown)
{
	HRESULT hr = IComponentDataImpl<CSnapIn, CUserComponent >::Initialize(pUnknown);
	if (FAILED(hr))
		return hr;

	CComPtr<IImageList> spImageList;

	if (m_spConsole->QueryScopeImageList(&spImageList) != S_OK)
	{
		ATLTRACE(_T("IConsole::QueryScopeImageList failed\n"));
		return E_UNEXPECTED;
	}
	// Load bitmaps associated with the scope pane
	// and add them to the image list
	// Loads the default bitmaps generated by the wizard
	// Change as required
	return LoadImages(spImageList);
}

HRESULT CSnapIn::LoadImages(IImageList* pImageList)
{
	HRESULT hr = E_FAIL;
	HBITMAP m_hSmallBmp = LoadBitmap(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_SMICONS));
	   if (m_hSmallBmp != NULL)
	   {
		  HBITMAP m_hLargeBmp = LoadBitmap(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_LGICONS));
		  if (m_hLargeBmp != NULL)
		  {
			 hr = pImageList->ImageListSetStrip((long*) m_hSmallBmp, (long*) m_hLargeBmp,0, RGB(0, 128, 128));
			 if (FAILED(hr))
				ATLTRACE(_T("IImageList::ImageListSetStrip failed\n"));
		  }
		  DeleteObject(m_hLargeBmp);
	   }
	  DeleteObject(m_hSmallBmp);
	 return hr;
	//return S_OK;
}